import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;

public class StudyLow extends JFrame implements ActionListener, Study {
//	public StudyLow Myself = this;
	private JPanel contentPane;
	private JComboBox comboBox = new JComboBox();
	private JButton btnStart;

	private JFrame showFrame;
	private JPanel contentPane2;
	private JLabel lblDay;
	private JLabel label;
	private JLabel lblShowVoca;
	private JButton btnGo;
	private String line;
	private String[] temparr2 = new String[4];
	private ArrayList<String> voca = new ArrayList<String>();
	boolean testFlg = false;

	private static int day;
	private String[] temparr = new String[30];
	public StudyLow() {
		dayChoice();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnStart)) {
			StudyLow.day = comboBox.getSelectedIndex() + 1;
			showVoca(StudyLow.day);
			dispose();
		}

	}

	@Override
	public int dayChoice() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		btnStart = new JButton("Start!");
		btnStart.addActionListener(this);
		btnStart.setBounds(145, 184, 129, 27);
		contentPane.add(btnStart);

		comboBox = new JComboBox();
		comboBox.addActionListener(this);
		for (int i = 1; i < temparr.length + 1; i++) {
			temparr[i - 1] = Integer.toString(i);
		}
		comboBox.setModel(new DefaultComboBoxModel(temparr));
		comboBox.setMaximumRowCount(30);
		comboBox.setBounds(201, 128, 53, 24);
		contentPane.add(comboBox);

		JLabel lblWhatDoYo = new JLabel("What do yo want day in Study?");
		lblWhatDoYo.setBounds(128, 89, 214, 27);
		contentPane.add(lblWhatDoYo);

		JLabel lblDay = new JLabel("Day");
		lblDay.setBounds(163, 128, 62, 27);
		contentPane.add(lblDay);

		return comboBox.getSelectedIndex() + 1;
	}

	@Override
	public void showVoca(int day) {
		
		try {
			BufferedReader br = new BufferedReader(new FileReader("low_voca.txt"));

			String temp = null;
			while ((line = br.readLine()) != null) {
				temparr2 = line.split("/");

				if (Integer.parseInt(temparr2[0]) == StudyLow.day) {
					temp = temparr2[1] + "  :  " + temparr2[2];
					voca.add(temp);
				}

				if (voca.size() == 10) {
					break;
				}
			}
			br.close();

			showFrame = new JFrame("Study");
			showFrame.setBounds(100, 100, 450, 300);

			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 100, 450, 300);
			contentPane2 = new JPanel();
			contentPane2.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane2);
			contentPane2.setLayout(null);

			lblDay = new JLabel("Day");
			lblDay.setBounds(53, 24, 38, 32);
			contentPane2.add(lblDay);

			label = new JLabel(Integer.toString(StudyLow.day));
			label.setBounds(92, 31, 62, 18);
			contentPane2.add(label);

			lblShowVoca = new JLabel(voca.remove(0));
			lblShowVoca.setBounds(14, 68, 418, 110);
			contentPane2.add(lblShowVoca);

			btnGo = new JButton("Go");
			btnGo.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					if (e.getSource().equals(btnGo) && !voca.isEmpty()) {
						lblShowVoca.setText(voca.remove(0));

					}
					if (voca.isEmpty() && testFlg == false) {
						JOptionPane.showMessageDialog(contentPane2,
								"Today's study is over.", "Study",
								JOptionPane.INFORMATION_MESSAGE);
						btnGo.setText("Test");
						testFlg = true;
					}
					if(testFlg == true){
						//test
					}

				}
			});
			btnGo.setBounds(327, 214, 105, 27);
			contentPane2.add(btnGo);

			showFrame.add(contentPane2);
			showFrame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void test(int day) {
		// TODO Auto-generated method stub

	}

}